package com.pdg.android.splorr;

import com.pdg.android.common.StateMachine;
import com.pdg.android.splorr.data.Game;
import com.pdg.android.splorr.data.enums.GameState;
import com.pdg.android.splorr.states.MainMenuState;
import com.pdg.android.splorr.states.PlayerInventoryState;
import com.pdg.android.splorr.states.RoomItemsState;
import com.pdg.android.splorr.states.RoomSummaryState;

import android.content.SharedPreferences;
import android.os.Bundle;

public class SplorrActivity extends StateMachine<Game,SplorrActivity,GameState> {
	private static final String GAME_PREFERENCES_FILE = "splore_preferences";
	private SharedPreferences preferences;
    public SplorrActivity() {
		super(new Game(), GameState.MAIN_MENU);
		putState(new MainMenuState(getData(),this));
		putState(new RoomSummaryState(getData(),this));
		putState(new RoomItemsState(getData(),this));
		putState(new PlayerInventoryState(getData(),this));
	}

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setPreferences(this.getSharedPreferences(GAME_PREFERENCES_FILE, MODE_PRIVATE));
    }
	
	private void setPreferences(SharedPreferences thePreferences) {
		preferences = thePreferences;
	}
	
	private SharedPreferences getPreferences(){
		return preferences;
	}

	@Override
	protected void onSetCurrent(GameState theCurrent) {
		getData().setCurrentState(theCurrent);
	}
	@Override
    protected void onResume(){
    	super.onResume();
    	getData().load(getPreferences());
    	setCurrent(getData().getCurrentState());
    }

	@Override
    protected void onPause(){
    	super.onPause();
    	getData().commit(getPreferences());
    }
}